package cn.eu.common.query.model;

import cn.eu.common.query.annotation.Condition;
import cn.eu.common.query.annotation.Join;
import cn.eu.common.query.annotation.QueryJoin;
import cn.eu.common.query.annotation.Table;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Getter
@Setter
public class JoinModel {

    private Join.Mode mode;

    private TableModel left;

    private TableModel right;

    private List<ConditionModel> on;

    public JoinModel(Map<String, TableModel> tableModelMap, TableModel mainTable, Join join) {
        this.mode = join.mode();
        Table left = join.mainTable();
        if(left.target() != Void.class) {
            this.left = new TableModel(left);
        } else {
            this.left = mainTable;
        }
        this.right = new TableModel(join.joinTable());
        tableModelMap.put(this.left.getAlias(), this.left);
        tableModelMap.put(this.right.getAlias(), this.right);
        this.on = new ArrayList<>();
        this.on.add(new ConditionModel(this.left, this.right));
        for (Condition condition : join.on()) {
            this.on.add(new ConditionModel(tableModelMap, mainTable.getAlias(), condition));
        }
    }
}
